﻿using System;
using System.Drawing;

namespace ClockwiseClient
{
    class ClassGraphDuogu : ClassGraph
    {

        public override void Paint(Graphics g, Rectangle rect)
        {
            this.rect = rect;
            if (format.LineAlignment != StringAlignment.Center) format.LineAlignment = StringAlignment.Center;
            if (format.Alignment != StringAlignment.Center) format.Alignment = StringAlignment.Center;
            String tips = "显示区域太小了";
            stringSize = g.MeasureString(tips, font);
            if (rect.Width < 640 || rect.Height < 480)
            {
                if (stringSize.Width > rect.Width)
                {
                    tips = "太小了";
                    stringSize = g.MeasureString(tips, font);
                }
                if (stringSize.Width > rect.Width || stringSize.Height > rect.Height) return;
                PointF pointF = new PointF(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2 + stringSize.Height / 10);
                g.DrawString(tips, font, Brushes.Cyan, pointF, format);
                return;
            }
            ShowDuogu(g, rect);
        }

        public override EnumGraph GetGraphType() { return EnumGraph.GRAPH_DUOGU; }

        private ClassGraphPrice price;

        public ClassGraphDuogu(ClassGraphPrice price)
        {
            this.price = price;
        }

        int graphCount = 6;

        void ShowDuogu(Graphics g, Rectangle rect)
        {
            string text;
            SizeF textSize;
            Point point = new Point();
            text = "多股同屏";
            textSize = g.MeasureString(text, fontTitle1);
            format.Alignment = StringAlignment.Near;
            int left = rect.Left + 4;
            point.X = left;
            point.Y = (int) (rect.Top + 4 + textSize.Height / 2 + textSize.Height / 10);
            g.DrawString(text, fontTitle1, Brushes.Yellow, point, format);

            ClassStockData classStockData = (ClassStockData)ClassStock.GetInstance();

            float graphTop = rect.Top + textSize.Height;
            float graphLeft = rect.Left + 10;
            float graphRight = rect.Right - 10;
            float graphBottom = rect.Bottom - 10;

            int w = (int)(graphRight - graphLeft) / (graphCount == 4 ? 2 : 3) - 10;
            int h = (int)(graphBottom - graphTop) / 2;
            int[] idxList = price.GetCursorTick(graphCount);
            for (int i = 0; i < graphCount; i++)
            {
                ClassStockItem si = classStockData.GetClassStockItem(idxList[i]);
                if (si == null) continue;
                int l, t;
                switch (graphCount)
                {
                    case 4:
                        switch (i)
                        {
                            default:
                            case 0:
                                t = (int)graphTop;
                                l = (int)graphLeft;
                                break;
                            case 1:
                                t = (int)graphTop;
                                l = (int)graphLeft + w + 10;
                                break;
                            case 2:
                                t = (int)graphTop + h;
                                l = (int)graphLeft;
                                break;
                            case 3:
                                t = (int)graphTop + h;
                                l = (int)graphLeft + w + 10;
                                break;
                        }
                        break;
                    case 6:
                    default:
                        switch (i)
                        {
                            default:
                            case 0:
                                t = (int)graphTop;
                                l = (int)graphLeft;
                                break;
                            case 1:
                                t = (int)graphTop;
                                l = (int)graphLeft + w + 10;
                                break;
                            case 2:
                                t = (int)graphTop;
                                l = (int)graphLeft + (w + 10) * 2;
                                break;
                            case 3:
                                t = (int)graphTop + h;
                                l = (int)graphLeft;
                                break;
                            case 4:
                                t = (int)graphTop + h;
                                l = (int)graphLeft + w + 10;
                                break;
                            case 5:
                                t = (int)graphTop + h;
                                l = (int)graphLeft + (w + 10) * 2;
                                break;
                        }
                        break;
                }
                Rectangle zstRect = new Rectangle(l, t+24, w, h-24);
                ClassStockTick classStockTick = classStockData.LoadStockTick(i);
                ClassDrawGraph.drawZoushitu(g, zstRect, si, classStockTick, true);
                text = si.code;
                textSize = g.MeasureString(text, fontTitle0);
                format.Alignment = StringAlignment.Near;
                point.X = l + 4;
                point.Y = t + 4 + (int)(textSize.Height / 2 + textSize.Height / 10);
                g.DrawString(text, fontTitle0, Brushes.Cyan, point, format);
                point.X += (int)textSize.Width;
                point.Y = t + 4 + (int)(textSize.Height / 2 + textSize.Height / 10);
                text = si.name;
                textSize = g.MeasureString(text, fontTitle1);
                g.DrawString(text, fontTitle0, classStockData.ZixuanguInclude(si.symbol) ? Brushes.Magenta : Brushes.Yellow, point, format);
            }
        }

        public override void MouseWheel(int delta)
        {
            price.MouseWheel(delta, graphCount);
        }
    }
}
